package com.junzhiit.nengji.app.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.app.service.GoodsService;
import com.junzhiit.nengji.app.web.exception.GoodsNotFoundException;
import com.junzhiit.nengji.app.web.exception.GoodsStatusErrorException;
import com.junzhiit.nengji.app.web.exception.ProfitOverflowException;
import com.junzhiit.nengji.app.web.exception.ShopAuditFailedException;
import com.junzhiit.nengji.app.web.exception.ShopAuditWaitException;
import com.junzhiit.nengji.app.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.app.web.util.ViewObjectUtil;
import com.junzhiit.nengji.app.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.app.web.vo.GoodsAddVO;
import com.junzhiit.nengji.app.web.vo.GoodsDetailsVO;
import com.junzhiit.nengji.app.web.vo.GoodsEditVO;
import com.junzhiit.nengji.app.web.vo.GoodsInfoVO;
import com.junzhiit.nengji.app.web.vo.GoodsListVO;
import com.junzhiit.nengji.app.web.vo.GoodsVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.app.web.vo.SkuContentVO;
import com.junzhiit.nengji.app.web.vo.SkuItemsVO;
import com.junzhiit.nengji.app.web.vo.SkuVO;
import com.junzhiit.nengji.app.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.app.web.vo.TagVO;
import com.junzhiit.nengji.bo.GoodsBO;
import com.junzhiit.nengji.bo.GoodsSkuItemsBO;
import com.junzhiit.nengji.bo.LabelBO;
import com.junzhiit.nengji.dao.CartDAO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.GoodsLabelItemsDAO;
import com.junzhiit.nengji.dao.GoodsSkuItemsDAO;
import com.junzhiit.nengji.dao.GoodsSystemTypeDAO;
import com.junzhiit.nengji.dao.GoodsTagDAO;
import com.junzhiit.nengji.dao.LabelDAO;
import com.junzhiit.nengji.dao.ModulesGoodsDAO;
import com.junzhiit.nengji.dao.ShopChainGoodsDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.SkuContentDAO;
import com.junzhiit.nengji.dao.TagDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Goods;
import com.junzhiit.nengji.model.GoodsLabelItems;
import com.junzhiit.nengji.model.GoodsSystemType;
import com.junzhiit.nengji.model.GoodsTag;
import com.junzhiit.nengji.model.ModulesGoods;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.SkuContent;
import com.junzhiit.nengji.model.Tag;
import com.junzhiit.nengji.model.enums.GoodsImagesType;
import com.junzhiit.nengji.model.enums.GoodsStatus;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.PostageType;
import com.junzhiit.nengji.model.enums.ShopIsPlatform;
import com.junzhiit.nengji.model.enums.Status;

@Service
public class GoodsServiceImpl extends BaseServiceImpl implements GoodsService {

	@Autowired
	private GoodsDAO goodsDAO;

	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;

	@Autowired
	private CartDAO cartDAO;

	@Autowired
	private ModulesGoodsDAO modulesGoodsDAO;

	@Autowired
	private GoodsSystemTypeDAO goodsSystemTypeDAO;

	@Autowired
	private ShopChainGoodsDAO shopChainGoodsDAO;

	@Autowired
	private GoodsLabelItemsDAO goodsLabelItemsDAO;

	@Autowired
	private GoodsTagDAO goodsTagDAO;
	
	@Autowired
	private SkuContentDAO skuContentDAO;
	
	@Autowired
	private GoodsSkuItemsDAO goodsSkuItemsDAO;
	
	@Autowired
	private LabelDAO labelDAO;
	
	@Autowired
	private TagDAO tagDAO;

	@Override
	public Map<String, Object> findPageAll(DataTablesParamVO dtp, GoodsListVO goodsListVO) {

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(goodsListVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		GoodsBO goodsBO = new GoodsBO();
		BeanUtils.copyProperties(goodsListVO, goodsBO);
		goodsBO.setShopId(shop.getId());

		List<GoodsBO> list = goodsDAO.findPageAll(goodsBO, dtp.getStart(), dtp.getLength(),shop.getIsPlatform());
		int totalCount = goodsDAO.getTotalCount(goodsBO,goodsListVO.getIsPlatform());
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO findByIdForOther(String id) {
		ResultVO result = new ResultVO();
		GoodsBO goodsBO = goodsDAO.findByIdForOther(id);
		if (goodsBO == null) {
			throw new GoodsNotFoundException("商品不存在");
		}
		GoodsInfoVO goodsInfoVO = new GoodsInfoVO();
		BeanUtils.copyProperties(goodsBO, goodsInfoVO);

		// 查询商品轮播图
		List<String> cardImgList = goodsDAO.getFileNameByCode(goodsBO.getCode(), GoodsImagesType.CARD);
		goodsInfoVO.setCardImgList(cardImgList);
		// 查询商品详情图
		List<String> detailImgList = goodsDAO.getFileNameByCode(goodsBO.getCode(), GoodsImagesType.DETAIL);
		goodsInfoVO.setDetailImgList(detailImgList);

		// 判断是否开启sku
		if (goodsInfoVO.getIsOpenSku() == Is.YES.toIntValue()) {

			List<GoodsSkuItemsBO> goodsSkuItemsBOs = goodsSkuItemsDAO.queryGoodsSkuItemsBO(goodsBO.getId());

			Map<String, SkuVO> map = new LinkedHashMap<String, SkuVO>();

			for (GoodsSkuItemsBO bo : goodsSkuItemsBOs) {
				SkuVO skuVO = map.get(bo.getSkuId());
				if (skuVO == null) {
					skuVO = new SkuVO();
					skuVO.setId(bo.getSkuId());
					skuVO.setName(bo.getName());
					skuVO.setIsOpenImage(bo.getIsOpenImage());
					List<SkuItemsVO> skuItemsList = new ArrayList<SkuItemsVO>();
					skuVO.setSkuItemsList(skuItemsList);
					map.put(skuVO.getId(), skuVO);
				}

				SkuItemsVO skuItemsVO = new SkuItemsVO();
				skuItemsVO.setId(bo.getSkuItemsId());
				skuItemsVO.setSkuId(bo.getSkuId());
				skuItemsVO.setStandard(bo.getStandard());
				skuItemsVO.setImage(bo.getImage());
				skuItemsVO.setIsOpenImage(bo.getIsOpenImage());
				skuVO.getSkuItemsList().add(skuItemsVO);
			}
			Collection<SkuVO> data = map.values();
			goodsInfoVO.setSkuList(data);

			// 查询skuContent
			List<SkuContent> SkuContentList = skuContentDAO.findSkuContentByGoodsId(goodsBO.getId());
			List<SkuContentVO> skuContentVOList = new ArrayList<SkuContentVO>();
			int stock = 0;
			List<Double> priceList = new ArrayList<Double>();
			for (SkuContent skuContent : SkuContentList) {
				SkuContentVO skuContentVO = new SkuContentVO();
				skuContent.setStandards(skuContent.getStandards().replace("+", ";"));
				BeanUtils.copyProperties(skuContent, skuContentVO);
				stock += (skuContent.getStock() == null) ? 0 : skuContent.getStock();
				priceList.add(skuContent.getPrice());
				skuContentVOList.add(skuContentVO);
			}
			goodsInfoVO.setStock(stock);
			if (priceList.size() > 0) {
				Double maxPrice = Collections.max(priceList);
				Double minPrice = Collections.min(priceList);
				if (maxPrice != minPrice) {
					goodsInfoVO.setPriceRange((minPrice + "-" + maxPrice));
				} else {
					goodsInfoVO.setPriceRange(minPrice.toString());
				}
			}
			goodsInfoVO.setSkuContentList(skuContentVOList);
		}
		
		//取得商品的参数详情
	    List<LabelBO> labelList = labelDAO.getGoodsAll(goodsBO.getId());
	    goodsInfoVO.setLabelList(labelList);
	    
	    //取得标签属性(服务承诺等)
	    List<TagVO> tagVOList = new ArrayList<TagVO>();
	    List<Tag> tagList = tagDAO.getAllByGoodsId(goodsBO.getId());
	    for(int i=0,t=tagList.size();i<t;i++){
	    	Tag tag = tagList.get(i);
	    	TagVO tagVO = new TagVO();
	    	BeanUtils.copyProperties(tag, tagVO);
	    	tagVOList.add(tagVO);
	    }
	    goodsInfoVO.setTagVOList(tagVOList);

		result.setData(goodsInfoVO);
		return result;
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO result = new ResultVO();
		Goods goods = goodsDAO.query(id);
		if (goods == null) {
			throw new GoodsNotFoundException("商品不存在");
		}
		GoodsDetailsVO goodsDetailsVO = new GoodsDetailsVO();
		BeanUtils.copyProperties(goods, goodsDetailsVO);

		// system_type
		List<String> systemTypes = goodsSystemTypeDAO.getGoodsSystemTypeByGoodsId(goods.getId());
		goodsDetailsVO.setSystemTypeId(systemTypes);

		// tag
		List<String> tagIds = goodsTagDAO.getAllByGoodsId(goods.getId());
		goodsDetailsVO.setTagIds(tagIds);

		result.setData(goodsDetailsVO);
		return result;
	}

	@Override
	public ResultVO add(GoodsAddVO goodsAddVO) {
		ResultVO result = new ResultVO();

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(goodsAddVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		// 判断返利是否在范围内
		int isOpenRebate = goodsAddVO.getIsOpenRebate();
		if (isOpenRebate == 1 && goodsAddVO.getProfit() <= shop.getProfit()) {
			throw new ProfitOverflowException("返利值小于店铺设置的默认值");
		}

		String maxCode = goodsDAO.queryMaxCode();
		String code = Integer.toString(Integer.parseInt(maxCode) + 1);
		Integer discount = goodsAddVO.getDiscount();
		if (discount == null) {
			goodsAddVO.setDiscount(0);
		}
		goodsAddVO.setCode(code);

		if (shop.getIsPlatform() == ShopIsPlatform.HEADQUARTERS.toIntStatus()) {
			goodsAddVO.setStatus(GoodsStatus.AUDIT.toIntStatus());
		}

		if (goodsAddVO.getPostageType() == PostageType.POSTAGE.toIntValue()) {
			goodsAddVO.setPostagePrice(0.00);
		}

		Goods goods = new Goods();
		BeanUtils.copyProperties(goodsAddVO, goods);
		goods.setShopId(shop.getId());
		Goods data = goodsDAO.insert(goods);

		// goodsSystemType
		if (StringUtils.isNoneBlank(data.getId())) {
			List<GoodsSystemType> list = new ArrayList<GoodsSystemType>();
			for (int i = 0; i < goodsAddVO.getSystemTypeId().size(); i++) {
				GoodsSystemType goodsSystemType = new GoodsSystemType();
				goodsSystemType.setGoodsId(data.getId());
				goodsSystemType.setSystemTypeId(goodsAddVO.getSystemTypeId().get(i));
				goodsSystemType.setCreatedTime(new Date());
				list.add(goodsSystemType);
			}
			goodsSystemTypeDAO.batchInsert(list);
		}

		// goodsLabelItems
		List<GoodsLabelItems> goodsLabelItemsList = JsonUtil.toListObject(goodsAddVO.getGoodsLabelItems(),
				GoodsLabelItems.class);
		if (goodsLabelItemsList != null && goodsLabelItemsList.size() > 0) {
			for (GoodsLabelItems goodsLabelItems : goodsLabelItemsList) {
				goodsLabelItems.setGoodsId(data.getId());
			}
			goodsLabelItemsDAO.batchInsert(goodsLabelItemsList);
		}

		// goodsTag
		if (goodsAddVO.getTagIds() != null && goodsAddVO.getTagIds().size() > 0) {
			List<GoodsTag> goodsTags = new ArrayList<GoodsTag>();
			for (String id : goodsAddVO.getTagIds()) {
				GoodsTag goodsTag = new GoodsTag();
				goodsTag.setGoodsId(data.getId());
				goodsTag.setTagId(id);
				goodsTags.add(goodsTag);
			}
			goodsTagDAO.batchInsert(goodsTags);
		}

		result.setData(data);
		return result;
	}

	@Override
	public ResultVO edit(GoodsEditVO goodsEditVO) {

		ResultVO result = new ResultVO();

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(goodsEditVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		Goods goods = new Goods();
		if (goodsEditVO.getPostageType() != null) {
			if (goodsEditVO.getPostageType() == PostageType.POSTAGE.toIntValue()) {
				goodsEditVO.setPostagePrice(0.00);
			}
		}
		BeanUtils.copyProperties(goodsEditVO, goods);

		if (shop.getIsPlatform() == Is.NO.toIntValue()) {
			goods.setStatus(GoodsStatus.AUDIT.toIntStatus());
		}

		// goodsSystemType
		if (StringUtils.isNoneBlank(goodsEditVO.getId())) {

			goodsSystemTypeDAO.deleteByGoodsId(goodsEditVO.getId());

			List<GoodsSystemType> list = new ArrayList<GoodsSystemType>();
			for (int i = 0; i < goodsEditVO.getSystemTypeId().size(); i++) {
				GoodsSystemType goodsSystemType = new GoodsSystemType();
				goodsSystemType.setGoodsId(goodsEditVO.getId());
				goodsSystemType.setSystemTypeId(goodsEditVO.getSystemTypeId().get(i));
				goodsSystemType.setCreatedTime(new Date());
				list.add(goodsSystemType);
			}
			goodsSystemTypeDAO.batchInsert(list);
		}

		// goodsLabelItems
		List<GoodsLabelItems> goodsLabelItemsList = JsonUtil.toListObject(goodsEditVO.getGoodsLabelItems(),
				GoodsLabelItems.class);
		if (goodsLabelItemsList != null) {
			for (GoodsLabelItems goodsLabelItems : goodsLabelItemsList) {
				goodsLabelItems.setGoodsId(goods.getId());
			}
		}
		GoodsLabelItems goodsLabelItems = new GoodsLabelItems();
		goodsLabelItems.setGoodsId(goods.getId());
		goodsLabelItemsDAO.deleteLabel(goodsLabelItems);
		if (goodsLabelItemsList != null) {
			goodsLabelItemsDAO.batchInsert(goodsLabelItemsList);
		}

		boolean data = goodsDAO.update(goods);

		// goodsTag
		List<GoodsTag> goodsTags = new ArrayList<GoodsTag>();
		if (goodsEditVO.getTagIds() != null && goodsEditVO.getTagIds().size() > 0) {
			for (String id : goodsEditVO.getTagIds()) {
				GoodsTag goodsTag = new GoodsTag();
				goodsTag.setGoodsId(goods.getId());
				goodsTag.setTagId(id);
				goodsTags.add(goodsTag);
			}
		}
		goodsTagDAO.deleteByGoodsId(goods.getId());
		goodsTagDAO.batchInsert(goodsTags);
		// cart
		cartDAO.updateStatus(goodsEditVO.getId(), Status.INACTIVE.toIntStatus());
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO batchUpdateStatus(String[] ids, Integer status, String sessionId) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		if (shop.getAuditStatus() == 0) {
			throw new ShopAuditWaitException("店铺等待审核中");
		}

		if (shop.getAuditStatus() == 1) {
			throw new ShopAuditFailedException("店铺审核未通过");
		}

		if (ids.length <= 0) {
			return result;
		}

		if (status != GoodsStatus.INACTIVE.toIntStatus() && status != GoodsStatus.ACTIVE.toIntStatus()) {
			return result;
		}
		boolean data = false;
		if (status == GoodsStatus.ACTIVE.toIntStatus()) {
			data = goodsDAO.batchUpdateStatus(ids, GoodsStatus.ACTIVE.toIntStatus());
		} else if (status == GoodsStatus.INACTIVE.toIntStatus()) {
			data = goodsDAO.batchUpdateStatus(ids, GoodsStatus.INACTIVE.toIntStatus());
		}
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO updateStatus(String id, Integer status, String sessionId) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = null;
		systemUserSessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		if (shop.getAuditStatus() == 0) {
			throw new ShopAuditWaitException("店铺等待审核中");
		}

		if (shop.getAuditStatus() == 1) {
			throw new ShopAuditFailedException("店铺审核未通过");
		}

		Goods goods = goodsDAO.query(id);
		if (goods == null) {
			throw new GoodsNotFoundException("商品不存在");
		}

		if (status != GoodsStatus.ACTIVE.toIntStatus() && status != GoodsStatus.INACTIVE.toIntStatus()
				&& status != GoodsStatus.AUDIT.toIntStatus()) {
			return result;
		}

		boolean data = false;
		if (status == GoodsStatus.ACTIVE.toIntStatus()) {
			data = goodsDAO.updateStatus(id, GoodsStatus.ACTIVE.toIntStatus());
		} else if (status == GoodsStatus.INACTIVE.toIntStatus()) {
			data = goodsDAO.updateStatus(id, GoodsStatus.INACTIVE.toIntStatus());
			cartDAO.updateStatus(id, Status.INACTIVE.toIntStatus());
		} else if (status == GoodsStatus.AUDIT.toIntStatus()) {
			data = goodsDAO.updateStatus(id, GoodsStatus.AUDIT.toIntStatus());
		}
		result.setData(data);

		return result;
	}

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, GoodsListVO goodsListVO) {
		GoodsBO goodsBO = new GoodsBO();
		BeanUtils.copyProperties(goodsListVO, goodsBO);
		
		if(goodsListVO.getCheckType()!=null && goodsListVO.getCheckType() == Is.YES.toIntValue()){
			SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
					.getHash(DIYKey.getSessionHashKey(goodsListVO.getSessionId()), SystemUserSessionVO.class);
			goodsBO.setChainId(systemUserSessionVO.getSystemId());
		}

		List<GoodsBO> list = goodsDAO.findPageAll(goodsBO, dtp.getStart(), dtp.getLength(),goodsListVO.getIsPlatform());
		int totalCount = goodsDAO.getTotalCount(goodsBO,goodsListVO.getIsPlatform());
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO editStatus(GoodsVO goodsVO) {
		ResultVO result = new ResultVO();
		Integer status = goodsVO.getStatus();
		if (status != GoodsStatus.AUDITSUCCESS.toIntStatus() && status != GoodsStatus.AUDITFAIL.toIntStatus()) {
			return result;
		}

		boolean data = false;
		if (status == GoodsStatus.AUDITSUCCESS.toIntStatus()) {
			data = goodsDAO.updateStatus(goodsVO.getId(), GoodsStatus.AUDITSUCCESS.toIntStatus());
		} else if (status == GoodsStatus.AUDITFAIL.toIntStatus()) {
			data = goodsDAO.updateStatus(goodsVO.getId(), GoodsStatus.AUDITFAIL.toIntStatus());
		}
		if (goodsVO.getAuditFailDescription() != null) {
			Goods goods = goodsDAO.query(goodsVO.getId());
			goods.setAuditFailDescription(goodsVO.getAuditFailDescription());
			goodsDAO.update(goods);
		}
		result.setData(data);

		return result;
	}

	@Override
	public ResultVO batchEditStatus(String[] ids, Integer status, String auditFailDescription) {
		ResultVO result = new ResultVO();
		if (ids.length <= 0) {
			return result;
		}

		if (status != GoodsStatus.AUDITSUCCESS.toIntStatus() && status != GoodsStatus.AUDITFAIL.toIntStatus()) {
			return result;
		}
		boolean data = false;
		if (status == GoodsStatus.AUDITSUCCESS.toIntStatus()) {
			data = goodsDAO.batchUpdateStatus(ids, GoodsStatus.AUDITSUCCESS.toIntStatus());
		} else if (status == GoodsStatus.AUDITFAIL.toIntStatus()) {
			data = goodsDAO.batchUpdateStatus(ids, GoodsStatus.AUDITFAIL.toIntStatus());
		}
		for (int i = 0; i < ids.length; i++) {
			Goods goods = goodsDAO.query(ids[i]);
			goods.setAuditFailDescription(auditFailDescription);
			goodsDAO.update(goods);
		}
		result.setData(data);
		return result;
	}

	@Override
	public Map<String, Object> goodsShopList(DataTablesParamVO dtp, String shopId, String name, Integer status) {
		GoodsBO goodsBO = new GoodsBO();
		Shop shop = shopDAO.query(shopId);
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		goodsBO.setShopId(shopId);
		goodsBO.setName(name);
		goodsBO.setStatus(status);
		List<GoodsBO> list = goodsDAO.findPageAll(goodsBO, dtp.getStart(), dtp.getLength(),shop.getIsPlatform());
		int totalCount = goodsDAO.getTotalCount(goodsBO,shop.getIsPlatform());
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO updateGoodsStatus(String id, String auditFailDescription) {
		ResultVO result = new ResultVO();
		Goods goods = goodsDAO.query(id);
		ModulesGoods modulesGoods = modulesGoodsDAO.query(id);
		if (goods == null) {
			throw new GoodsNotFoundException("商品不存在");
		}

		if (goods.getStatus() != GoodsStatus.ACTIVE.toIntStatus()) {
			throw new GoodsStatusErrorException("商品不为上架状态");
		}
		if (StringUtils.isNotBlank(auditFailDescription)) {
			goods.setStatus(GoodsStatus.AUDITFAIL.toIntStatus());
			goods.setAuditFailDescription(auditFailDescription);
		}
		goodsDAO.update(goods);
		// goodsDAO.updateStatus(id, GoodsStatus.AUDITFAIL.toIntStatus());

		if (modulesGoods != null) {
			modulesGoods.setStatus(GoodsStatus.AUDITFAIL.toIntStatus());
			modulesGoodsDAO.update(modulesGoods);
		}

		return result;
	}

	@Transactional
	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		// 商品删除
		goodsDAO.updateToDelete(id);

		// 购物车失效
		cartDAO.updateStatus(id, Status.INACTIVE.toIntStatus());

		// 模块商品移除
		modulesGoodsDAO.deleteByGoodsId(id);

		// 连锁品牌商品移除
		shopChainGoodsDAO.deleteByGoodsId(id);

		return result;
	}

	@Override
	public ResultVO batchDeleteChainGoods(String[] ids) {
		ResultVO result = new ResultVO();
		for (String id : ids) {
			delete(id);
		}
		return result;
	}

	@Override
	public Map<String, Object> findByProgram(DataTablesParamVO dtp, GoodsListVO goodsListVO) {
		GoodsBO goodsBO = new GoodsBO();
		BeanUtils.copyProperties(goodsListVO, goodsBO);
		
		List<GoodsBO> list = goodsDAO.findPageAll(goodsBO, dtp.getStart(), dtp.getLength(),ShopIsPlatform.OWN.toIntStatus());
		int totalCount = goodsDAO.getTotalCount(goodsBO,ShopIsPlatform.OWN.toIntStatus());
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
}
